﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Threading;
using System.Windows.Browser;

namespace SingleFileUpload.Providers
{
	public class UserFile : INotifyPropertyChanged
	{
		private string _fileName;
		private bool _isDeleted = false;
		private Stream _fileStream;
		private Constants.FileStates _state = Constants.FileStates.Pending;
		private double _bytesUploaded = 0;
		private double _fileSize = 0;
		private int _percentage = 0;
		private IFileUploader _fileUploader;
		private string _initParams;
		private string _fileRealName = string.Empty;

		public Dispatcher UIDispatcher { get; set; }
		//public bool HttpUploader { get; set; }
		//public string UploadHandlerName { get; set; }

		public string UploadHandlerURL { get; set; }

		[ScriptableMember()]
		public string FileName
		{
			get { return _fileName; }
			set
			{
				_fileName = value;
				NotifyPropertyChanged("FileName");
			}
		}

		[ScriptableMember()]
		public string FileRealName
		{
			get
			{
				return _fileRealName;
			}
			set
			{
				_fileRealName = value;
				NotifyPropertyChanged("FileRealName");
			}
		}

		public Constants.FileStates State
		{
			get { return _state; }
			set
			{
				_state = value;

				NotifyPropertyChanged("State");
			}
		}

		[ScriptableMember()]
		public string StateString
		{
			get { return _state.ToString(); }
		}

		public bool IsDeleted
		{
			get { return _isDeleted; }
			set
			{
				_isDeleted = value;

				if (_isDeleted)
					CancelUpload();

				NotifyPropertyChanged("IsDeleted");
			}
		}

		public Stream FileStream
		{
			get { return _fileStream; }
			set
			{
				_fileStream = value;

				if (_fileStream != null)
					_fileSize = _fileStream.Length;
			}
		}

		public double FileSize
		{
			get
			{
				return _fileSize;
			}
		}

		public double BytesUploaded
		{
			get { return _bytesUploaded; }
			set
			{
				_bytesUploaded = value;

				NotifyPropertyChanged("BytesUploaded");

				Percentage = (int)((value * 100) / FileSize);

			}
		}

		[ScriptableMember()]
		public int Percentage
		{
			get { return _percentage; }
			set
			{
				_percentage = value;
				NotifyPropertyChanged("Percentage");
			}
		}

		public string ErrorMessage { get; set; }

		public string InitParams
		{
			get { return _initParams; }
			set { _initParams = value; }
		}

		public void Upload()
		{
			this.State = Constants.FileStates.Uploading;

			//if(HttpUploader)
			if (_fileUploader == null)
			{
				_fileUploader = new HttpFileUploader(this, UploadHandlerURL);
			}
			//else
			//_fileUploader = new WcfFileUploader(this);

			_fileUploader.StartUpload(_initParams);
			_fileUploader.UploadFinished += new EventHandler(fileUploader_UploadFinished);
		}

		public void CancelUpload()
		{
			if (_fileUploader != null && this.State == Constants.FileStates.Uploading)
			{
				_fileUploader.CancelUpload();

				this.FileStream = null;
			}
		}

		public void PauseUpload()
		{
			if (_fileUploader != null && this.State == Constants.FileStates.Uploading)
			{
				this.State = Constants.FileStates.Pause;
				this._fileUploader.CancelUpload();

				//this.FileStream = null;
			}
		}

		public void ResumeUpload()
		{
			if (_fileUploader != null && this.State == Constants.FileStates.Continue)
			{
				this.State = Constants.FileStates.Uploading;
				_fileUploader.ResumeUpload();
			}
		}

		private void fileUploader_UploadFinished(object sender, EventArgs e)
		{
			_fileUploader = null;

			this.State = Constants.FileStates.Finished;
		}


		#region INotifyPropertyChanged Members

		private void NotifyPropertyChanged(string prop)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(prop));
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion
	}
}
